import os
import random 
 
xmlfilepath=r'./Boxes'
saveBasePath=r"./imageSets"

temp_xml = os.listdir(xmlfilepath)
total_xml = []
for xml in temp_xml:
    if xml.endswith(".xml"):
        total_xml.append(xml)

num=len(total_xml)  
list_xml = [i for i in range(num)]  
new_list = random.sample(list_xml,num)  
train_size = int(num * 2 / 3)
val_size = num - train_size 

ftrain = open(os.path.join(saveBasePath,'train.txt'), 'w')  
fval = open(os.path.join(saveBasePath,'val.txt'), 'w')  

for i in range(train_size):
    name=total_xml[new_list[i]][:-4]+'\n'  
    ftrain.write(name)

for i in range(train_size, num):
    name=total_xml[new_list[i]][:-4]+'\n'  
    fval.write(name)  
 
ftrain.close()  
fval.close()  